package thread_pool_executor;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author: 史大拿
 * @create: 2022-10-21 09:21
 * TODO
 **/
public class TPEClient3 {
    private static final int COUNT = 200;

    public static void main(String[] args) {

        LinkedBlockingQueue<Runnable> blockQueue =
                new LinkedBlockingQueue<>(100);


        ThreadPoolExecutor threadPoolExecutor =
                new ThreadPoolExecutor(4, // 核心线程数
                        10, // 最大线程数
                        10, // 失效时间
                        TimeUnit.SECONDS, // 失效单位
                        blockQueue, // 队列
                        new ThreadFactory() { // 线程工厂
                            @Override
                            public Thread newThread(Runnable r) {
                                return new Thread(r);
                            }
                        },
                        // 失败策略
                        new ThreadPoolExecutor.AbortPolicy()
                );

        for (int i = 0; i < COUNT; i++) {
            int finalI = i;
            Runnable runnable = new Runnable() {
                @Override
                public void run() {

                    try {
                        Thread.sleep(2000);
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        System.out.println("i:" + finalI +
                                "\t线程:" + Thread.currentThread().getName());
                    }
                }
            };

            threadPoolExecutor.execute(runnable);
        }
    }
}
